import SpdgBI.Common.DBUtils;
import SpdgBI.PhErp.*;
import oracle.sql.CLOB;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;

import static com.mchange.v2.c3p0.dbms.OracleUtils.createTemporaryCLOB;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;


public class Test {
    public static void main(String[] args) {

        //FinancialStmts.PumpData("0109","","2017-08-01","2017-08-31");
        //FinancialCommitFlag();
//        CarryForward pb = new CarryForward();
//        pb.PumpHisDataAll();
//         Topic pj = new Topic();
//        pj.PumpData("010500");
        //Inventory it = new Inventory();
        //it.PumpData("010200");
//        Book bk = new Book();
//        bk.PumpDataAll();
//        DBUtils.DumpMetaDataAll("BOOK","BASE_PUB_BOOK");
        //DBUtils.DumpMetaDataAll("TOPIC","BASE_PUB_TOPIC");
        //DBUtils.DumpMetaDataAll("PROJECT","BASE_PUB_PROJECT");
        DBUtils.DumpMetaDataAll("AddPrintProject","BASE_PUB_ADDPRINTPROJECT");
//          DBUtils.DumpMetaData("SCRIPTSOURCE","BASE_PUB_SCRIPTSOURCE","0104");
//        DBUtils.DumpMetaDataAll("ISBN","BASE_PUB_ISBN");
//        DBUtils.DumpMetaDataAll("SERIES","BASE_PUB_SERIES");
//        DBUtils.DumpMetaDataAll("BOOKTYPE","BASE_PUB_BOOKTYPE");
//        DBUtils.DumpMetaDataAll("DEPARTMENT","BASE_PUB_DEPARTMENT");
//        DBUtils.DumpMetaDataAll("EMPLOYEE","BASE_PUB_EMPLOYEE");
//        DBUtils.DumpMetaDataAll("FORM","BASE_PUB_FORM");
//        DBUtils.DumpMetaDataAll("TOPICCOMMENT","BASE_PUB_TOPICCOMMENT");
 //       DBUtils.DumpMetaDataAll("ConstGroupSet","BASE_PUB_CONSTGROUPSET");
 //       DBUtils.DumpMetaDataAll("TEXTBOOKTYPE","BASE_PUB_TEXTBOOKTYPE");
 //       DBUtils.DumpMetaDataAll("SPECFORMAT","BASE_PUB_SPECFORMAT");
 //       DBUtils.DumpMetaDataAll("GENREWORD","BASE_PUB_GENREWORD");
//        DBUtils.DumpMetaDataAll("BOOKCURRENTSTORAGE","BASE_PUB_BOOKCURRENTSTORAGE");
//        DBUtils.DumpMetaDataAll("FINANCETYPE","BASE_PUB_FINANCETYPE");


//        fb.PumpDataAll();
        //RunJob();
        //DataPump_Bookcard_all();

        /*
        try{
            Test TestJob = new Test();
            TestJob.run();
        }catch (Exception ex){
            ex.printStackTrace();
        }
        */

    }
    public void run() throws Exception {
        Logger log = LoggerFactory.getLogger(TestJob.class);

        // First we must get a reference to a scheduler
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = null;
        try {
            sched = sf.getScheduler();
        } catch (NoClassDefFoundError e) {
            log.error(" Unable to load a class - most likely you do not have jta.jar on the classpath. If not present in the examples/lib folder, please " +
                    "add it there for this sample to run.", e);
            return;
        }

        log.info("------- Initialization Complete -----------");

        log.info("------- (Not Scheduling any Jobs - relying on XML definitions --");

        log.info("------- Starting Scheduler ----------------");

        // start the schedule
        sched.start();

        log.info("------- Started Scheduler -----------------");

        log.info("------- Waiting five minutes... -----------");

        // wait five minutes to give our jobs a chance to run
        try {
            Thread.sleep(300L * 1000L);
        } catch (Exception e) {
            //
        }

        // shut down the scheduler
        log.info("------- Shutting Down ---------------------");
        sched.shutdown(true);
        log.info("------- Shutdown Complete -----------------");

        SchedulerMetaData metaData = sched.getMetaData();
        log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
    }

    public static void RunJob(){
        try {
            //创建scheduler
             Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            //定义一个Trigger
            Trigger trigger = newTrigger()
                    .withIdentity("trigger1", "group1") //定义name/group
                    .startNow()//一旦加入scheduler，立即生效
                    .withSchedule(simpleSchedule() //使用SimpleTrigger
                            .withIntervalInSeconds(1) //每隔一秒执行一次
                            .repeatForever()) //一直执行，奔腾到老不停歇
                    .build();

            //定义一个JobDetail
            JobDetail job = newJob(TestJob.class) //定义Job类为HelloQuartz类，这是真正的执行逻辑所在
                    .withIdentity("job1", "group1") //定义name/group
                    .usingJobData("name", "quartz") //定义属性
                    .build();

            //加入这个调度
            scheduler.scheduleJob(job, trigger);

            //启动之
            scheduler.start();

            //运行一段时间后关闭
            Thread.sleep(10000);
            scheduler.shutdown(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void InsertOne(){
        StringBuffer sql = new StringBuffer();
        sql.append("insert into base_pub_book (UNITCODE,CONTENT) values (?,?)");
        Connection con = null;
        try {
            // 关闭事务自动提交
            con = DBUtils.getConnection("SPDGBI");
            con.setAutoCommit(false);
            final int batchSize = 10000;
            int count = 0;
            Long startTime = System.currentTimeMillis();
            PreparedStatement pst = con.prepareStatement(sql.toString());
            //OraclePreparedStatement oracleStmt = pst.unwrap(OraclePreparedStatement.class);
            //OracleConnection ocon = con.unwrap(OracleConnection.class);

            oracle.sql.CLOB  clob = createTemporaryCLOB(con, false, oracle.sql.CLOB.DURATION_SESSION);
            clob.open(oracle.sql.CLOB.MODE_READWRITE);
            clob.setString(3, "llll");
            pst.setClob(2, clob);
            pst.setString(1,"0107");
           /*for (int i = 0; i < list.size(); i++) {
                ExLog exLog = (ExLog) list.get(i);
                pst.setString(1, exLog.getExLogId());
                pst.setString(2, exLog.getExLogDate());
                // 把一个SQL命令加入命令列表
                pst.addBatch();
                if (++count % batchSize == 0) {
                    pst.executeBatch();
                    count = 0;
                }
            }*/

            // 执行批量更新
            pst.addBatch();
            pst.executeBatch();
            // 语句执行完毕，提交本事务
            con.commit();
            Long endTime = System.currentTimeMillis();
            System.out.println("用时：" + (endTime - startTime));
            pst.close();
            con.close();
            CLOB.freeTemporary(null);

        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

}
